package test;

import java.util.Stack;

public class StackQueue<T> {
    // 作为入队序列
    private Stack<T> inStack = new Stack<T>();
    // 作为出队序列
    private Stack<T> outStack = new Stack<T>();

    public void push(T node) {
        //while (!outStack.empty()) {
        //    inStack.push(outStack.pop());
        //}
        inStack.push(node);
    }

    public T pop() {
        //while (!inStack.empty()) {
        //    outStack.push(inStack.pop());
        //}
        //return outStack.pop();
        // 出队时，如果outStack不空直接取，空了，再从inStack填充outStack
        if(outStack.isEmpty()) {
            while (!inStack.empty()) {
                outStack.push(inStack.pop());
            }
        }
        return outStack.pop();
    }

    public static void main(String[] args) {

        StackQueue<Integer> so = new StackQueue<>();
        so.push(1);
        so.push(2);
        so.push(3);

        //1 2 3
        //stack 3 2 1
        //

        int ret = so.pop();
        System.out.println("出队元素是:" + ret);
        ret = so.pop();
        System.out.println("出队元素是:" + ret);
        so.push(4);
        ret = so.pop();
        System.out.println("出队元素是:" + ret);
        so.push(5);
        so.push(6);
        ret = so.pop();
        System.out.println("出队元素是:" + ret);
        ret = so.pop();
        System.out.println("出队元素是:" + ret);
        ret = so.pop();
        System.out.println("出队元素是:" + ret);
    }
}
